/*
 * Created by Jaren at 2023/6/1 15:20
 */
import 'dart:typed_data';

import 'package:binary/binary.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/common/bms_info.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class V14BMSAllRealTimeInfo extends BMSAllRealTimeInfo {
  V14BMSAllRealTimeInfo(ProductModel model, VersionWrapper version) : super(model, version, true) {
    generalInfo = V14BMSGeneralInfo(model, version);
    state = BMSState(model, version);
    warning = V14BMSWarning(model, version);
    protection = V14BMSProtection(model, version);
    error = BMSError(model, version);
  }

  @override
  V14BMSGeneralInfo get generalInfo => super.generalInfo as V14BMSGeneralInfo;

  @override
  V14BMSWarning get warning => super.warning as V14BMSWarning;

  @override
  V14BMSProtection get protection => super.protection as V14BMSProtection;
}

class V14BMSCellsVoltage extends BMSCellsVoltage {
  V14BMSCellsVoltage(super.apiVersion, super.version);

  @override
  int batteryCount() {
    return 32;
  }
}

class V14BMSGeneralInfo extends BMSGeneralInfo {
  V14BMSGeneralInfo(super.model, super.version);

  // 每个电池4个温度传感器
  @override
  int cellTempSensorCount() => 4;
}

class V14BMSFixedInfo extends BMSFixedInfo {
  BatteryCellType batteryCellType = BatteryCellType.B50GB;

  V14BMSFixedInfo(super.model, super.version);

  @override
  void initCompact() {
    compact = DefaultV14BMSFixedInfo();
  }
}

class DefaultV14BMSFixedInfo extends DefaultBMSFixedInfoDelegate<V14BMSFixedInfo> {
  @override
  void fromBytes(V14BMSFixedInfo output, Uint8ClampedList bytes, int offset) {
    super.fromBytes(output, bytes, offset);
    offset += 2 + 2 + 2 + 2 + 4 + 4 + 2;
    var batteryCellType = ByteUtil.bitValue(bytes[offset], 0, 3);
    if (batteryCellType.value == 1) output.batteryCellType = BatteryCellType.B50S;
  }
}

class V14BMSProtection extends BMSProtection {
  Uint4 largeVoltageDiffOnCharge = Uint4.zero;

  V14BMSProtection(super.model, super.version);

  @override
  void initCompact() {
    compact = _DefaultV14BMSProtectionCompact();
  }

  @override
  String toString() {
    return 'V14BMSProtection{largeVoltageDiffOnCharge: $largeVoltageDiffOnCharge, ${super.toString()}}';
  }
}

class _DefaultV14BMSProtectionCompact extends DefaultBMSProtectionCompact<V14BMSProtection> {
  @override
  void fromBytes(V14BMSProtection output, Uint8ClampedList bytes, int offset) {
    super.fromBytes(output, bytes, offset);
    offset += 2;
    output.largeVoltageDiffOnCharge = ByteUtil.bitValue(bytes[offset], 0, 1);
  }
}

class V14BMSWarning extends BMSWarning {
  Uint4 largeVoltageDiffOnCharge = Uint4.zero;

  V14BMSWarning(super.model, super.version);

  @override
  void initCompact() {
    compact = _DefaultV14BMSWarningCompact();
  }

  @override
  String toString() {
    return 'V14BMSWarning{largeVoltageDiffOnCharge: $largeVoltageDiffOnCharge, ${super.toString()}}';
  }
}

class _DefaultV14BMSWarningCompact extends DefaultBMSWarningCompact<V14BMSWarning> {
  @override
  void fromBytes(V14BMSWarning output, Uint8ClampedList bytes, int offset) {
    super.fromBytes(output, bytes, offset);
    offset++;
    output.largeVoltageDiffOnCharge = ByteUtil.bitValue(bytes[offset], 6, 1);
  }
}
